//所有 table 实例均会有效，这样就不用在每个 table.render() 都设置一次 token
// layui.table.set({
//     headers: {
//         Author: 'Leamus',
//         Authorization: getUserInfo() != undefined ? getUserInfo()["token"] : ''
//     }
// });

// layui.upload.set({
//     headers: {
//         Author: 'Leamus',
//         Authorization: getUserInfo() != undefined ? getUserInfo()["token"] : ''
//     }
// })

// 设置请求默认值
layui.$.ajaxSetup({
    beforeSend: function (xhr) { //可以设置自定义标头
        if (getUserInfo() != undefined) {
            xhr.setRequestHeader('Author', 'Leamus');
            xhr.setRequestHeader('Authorization', getUserInfo() != undefined ? getUserInfo()["token"] : '');
        }
    },
    complete: function (xhr) {
        // 设置登陆拦截
        if (xhr.status === 401) {
            let ret = xhr['responseJSON'] !== undefined ? xhr['responseJSON'] : [];
            if (ret['code'] === undefined || ret['code'] != 0) {
                setUserInfo(null);
                location.href = lms_config.base_path + 'index.html'
            }
            setUserInfo({
                isLogin: true,
                "token": ret.data.token,
                info: ret.data.info,
                rules: ret.data.rules,
            });
            //继续完成当前访问
            layui.$.ajax(this)
        }
        if (xhr.status === 403) {
            window.errors('403.html')
        }
        if (xhr.status === 404) {
            window.errors('404.html')
        }
        if (xhr.status === 500) {
            window.errors('500.html')
        }
    },
});

window.errors = function (html) {
    let MODULE_PATH = lms_config.base_path + '/view/error/' + html;
    layer.open({
        type: 2,
        title: false,
        skin: 'layui-layer-win10', // 2.8+
        shadeClose: true, // 点击遮罩区域，关闭弹层
        closeBtn: false,
        maxmin: false,
        shade: 0.1,
        area: ['100%', '100%'],
        content: MODULE_PATH,
        cancel: function () {
        }
    });
}
